﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.User;
using PowerCreator.LMS.User.Entity;

public partial class sysadmin_UserManager_TeacherManager : PowerCreator.LMS.Web.Base.BasePage
{
    public User opUser = new User();
    Job opJob = new Job();
    Duty opDuty = new Duty();
    Department opDepar = new Department();
    PowerCreator.LMS.Web.Project.ProjectUser objProjectUser = new PowerCreator.LMS.Web.Project.ProjectUser();
    //PowerCreator.LMS.ViewQuery.ViewQuery ov = new PowerCreator.LMS.ViewQuery.ViewQuery();
    LogicServer.NetMeetingClass objNetMeetingClass;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["SearchType"] = "0";  //0简单搜索1高级查询
            ViewState["OrderBy"] = "";  //排序
            ViewState["OrderType"] = " desc ";//排序方式
            BinderSex();
            //btnDel.Visible = IsLicensed("DelTeacher");
            InitPage();
            //  BinderJob();
            //  BinderDuty();
            BinderDepartment();
            BinderUserState();

            string search = GetSearch();
            BinderPager(search);
            BinderRpt(search, 1);

            if (PowerCreator.LMS.Web.Config.WebConfig.IsUseNetMeeting)
            {
                objNetMeetingClass = new LogicServer.NetMeetingClass();
            }

        }
    }
    protected override void InitPage()
    {
        lbl_InfoTitle_TeacherManager.Text = GetLanageValueByKey("lbl_TeacherManager_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_TeacherManager_Name.Text = GetLanageValueByKey("lbl_TeacherManager_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_TeacherManager_LoadingName.Text = GetLanageValueByKey("lbl_TeacherManager_LoadingName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_TeacherManager_DepartmentName.Text = GetLanageValueByKey("lbl_TeacherManager_DepartmentName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_TeacherManager_Sex.Text = GetLanageValueByKey("lbl_TeacherManager_Sex", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_StateName.Text = GetLanageValueByKey("RoleUser_gv_c1", PowerCreator.LMS.Language.LanagetItemType.Page);

        // lbl_TeacherManager_JopName.Text = GetLanageValueByKey("lbl_TeacherManager_JopName", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_TeacherManager_DutyName.Text = GetLanageValueByKey("lbl_TeacherManager_DutyName", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Search.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Add.Text = GetLanageValueByKey("btn_TeacherManager_AddTeacher", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnLock.Text = GetLanageValueByKey("btnLock", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnAuditing.Text = GetLanageValueByKey("btnAuditing", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnDel.Text = GetLanageValueByKey("btnDel", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnDel.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
        chkAll.Text = GetLanageValueByKey("RoleUser_rdbtn_YesNoAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("SetBaseAction_rdbtn_All", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_SortSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_pwd.Text = GetLanageValueByKey("btn_defaultpwd", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_pwd.Attributes.Add("onClick", "javascript:return confirm('" + GetLanageValueByKey("lbl_OnlineUser_Upt_pwd_Msg", PowerCreator.LMS.Language.LanagetItemType.Page) + "');");
        btn_ExportUser.Text = GetLanageValueByKey("btn_ExportUser", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();

        if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddTeacher))
            btn_Add.Visible = true;
        if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.DelTeacher))
            btnDel.Visible = true;

    }
    #region 数据绑定
    //private void BinderJob()
    //{
    //    string search = "";
    //    DataTable dt = opJob.Query(search);
    //    ddl_Job.DataSource = dt;
    //    ddl_Job.DataTextField = "TeajobName";
    //    ddl_Job.DataValueField = "TeajobID";
    //    ddl_Job.DataBind();
    //    ListItem li = new ListItem();
    //    li.Text = "不限职位";
    //    li.Value = "0";
    //    ddl_Job.Items.Insert(0,li);
    //}
    //private void BinderDuty()
    //{
    //    string search = "";
    //    DataTable dt = opDuty.Query(search);
    //    ddl_Duty.DataSource = dt;
    //    ddl_Duty.DataTextField = "TeadutyName";
    //    ddl_Duty.DataValueField = "TeadutyID";
    //    ddl_Duty.DataBind();

    //    ListItem li = new ListItem();
    //    li.Text = "不限职称";
    //    li.Value = "0";
    //    ddl_Duty.Items.Insert(0, li);
    //}
    protected void BinderSex()
    {
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("EditUser_desc29", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "2";

        ListItem li0 = new ListItem();
        li0.Text = GetLanageValueByKey("EditUser_desc27", PowerCreator.LMS.Language.LanagetItemType.Page);
        li0.Value = "0";
        ListItem li1 = new ListItem();
        li1.Text = GetLanageValueByKey("EditUser_desc28", PowerCreator.LMS.Language.LanagetItemType.Page);
        li1.Value = "1";
        ddl_Sex.Items.Add(li);
        ddl_Sex.Items.Add(li0);
        ddl_Sex.Items.Add(li1);
    }

    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lbtn_del = (LinkButton)e.Item.FindControl("lnkbtn_del");
            LinkButton lbtn_upt = (LinkButton)e.Item.FindControl("lnkbtn_password");
            lbtn_del.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
            lbtn_upt.Attributes.Add("onClick", "javascript:return confirm('" + GetLanageValueByKey("lbl_OnlineUser_Upt_pwd_Msg", PowerCreator.LMS.Language.LanagetItemType.Page) + "');");
        }
    }
    //绑定部门信息
    private void BinderDepartment()
    {
        string search = "DepartmentType = 1";
        DataTable dt = opDepar.Query(search);
        ArrayList list = new ArrayList();
        list = opDepar.GetDepartmentTree(dt);

        ddl_Department.DataSource = list;
        ddl_Department.DataTextField = "Text";
        ddl_Department.DataValueField = "Value";
        ddl_Department.DataBind();

        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("Msg_ManagerQuestion_PleaseCheck", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "0";
        ddl_Department.Items.Insert(0, li);
    }
    protected void BinderUserState()
    {

        ListItem li00 = new ListItem();
        li00.Text = GetLanageValueByKey("EditUser_desc29", PowerCreator.LMS.Language.LanagetItemType.Page);
        li00.Value = "-1";
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("AddCourseInfo_db_d2", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "0";
        ListItem li0 = new ListItem();
        li0.Text = GetLanageValueByKey("State_Msg2", PowerCreator.LMS.Language.LanagetItemType.Page);
        li0.Value = "1";
        ListItem li1 = new ListItem();
        li1.Text = GetLanageValueByKey("State_Msg1", PowerCreator.LMS.Language.LanagetItemType.Page);
        li1.Value = "2";
        ddl_State.Items.Add(li00);
        ddl_State.Items.Add(li);
        ddl_State.Items.Add(li0);
        ddl_State.Items.Add(li1);
    }

    #endregion

    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();
        sbSearch.Append(" 1=1 ");

        if (ViewState["SearchType"].ToString() == "1")
        {
            if (!string.IsNullOrEmpty(txt_RealName.Text))
            {
                sbSearch.Append(string.Format(" and TeacherName like '%{0}%' ", txt_RealName.Text));
            }
            if (!string.IsNullOrEmpty(txt_UserName.Text))
            {
                sbSearch.Append(string.Format(" and UserName like '%{0}%' ", txt_UserName.Text));
            }
            //if (ddl_Job.SelectedValue.ToString() != "0")
            //{
            //    sbSearch.Append(string.Format(" FKTeacherTeajobID = {0} and ", ddl_Job.SelectedValue.ToString()));
            //}
            //if (ddl_Duty.SelectedValue.ToString() != "0")
            //{
            //    sbSearch.Append(string.Format(" FKTeacherTeadutyID = {0} and ", ddl_Duty.SelectedValue.ToString()));
            //}
            if (ddl_Department.SelectedValue.ToString() != "0")
            {
                sbSearch.Append(string.Format(" and FKTeacherDepartmentID = {0} ", ddl_Department.SelectedValue.ToString()));
            }
            if (ddl_Sex.SelectedValue.ToString() != "2")
            {
                sbSearch.Append(string.Format(" and TeacherSex = {0} ", ddl_Sex.SelectedValue.ToString()));
            }
            if (ddl_State.SelectedValue.ToString() != "-1")
            {
                sbSearch.Append(string.Format(" and UserIscheck = {0}  ", ddl_State.SelectedValue.ToString()));
            }

        }
        else if (ViewState["SearchType"].ToString() == "0")
        {
            sbSearch.Append(string.Format(" and TeacherName like '%{0}%' or UserName like '%{0}%' ", txt_SearchKey.Text));
        }

        return sbSearch.ToString();
    }
    private void BinderPager(string search)
    {
        //ov.Query(search);
        //ov.Count(search);
        AspNetPager1.RecordCount = opUser.QueryVMAccountInfomationTeacherCount(search);
        AspNetPager1.PageSize = LMSPageSize;
    }
    private void BinderRpt(string search, int pageIndex)
    {
        string orderlist = ViewState["OrderBy"].ToString();
        if (!string.IsNullOrEmpty(orderlist))
            search += " order by " + orderlist + ViewState["OrderType"].ToString();
        DataTable dt = opUser.QueryVMAccountInfomaitionTeacher(search, LMSPageSize, pageIndex);
        rptUser.DataSource = dt;
        rptUser.DataBind();
        PreAspNetPager(AspNetPager1);
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRpt(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        //搜索用户
        ViewState["SearchType"] = "1";
        string search = GetSearch();
        BinderPager(search);
        BinderRpt(search, 1);
    }
    protected void btnAuditing_Click(object sender, EventArgs e)
    {
        //审核用户
        for (int i = 0; i < rptUser.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptUser.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
            {
                Label lblid = (Label)rptUser.Items[i].FindControl("lblid");
                VOTUser vo = opUser.LoadUser(int.Parse(lblid.Text));
                if (vo != null)
                {
                    if (vo.UserIscheck != 1)
                    {
                        vo.UserIscheck = 1;
                        opUser.Update(vo);
                    }
                }
            }
        }
        string search = GetSearch();
        BinderRpt(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        //删除选中
        for (int i = 0; i < rptUser.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptUser.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
            {
                Label lblid = (Label)rptUser.Items[i].FindControl("lblid");
                DataTable dt_ProjectClassUser = objProjectUser.QueryProjectClassUser(" FKTrainclastuUserID=" + lblid.Text);

                int UserID = int.Parse(lblid.Text);
                int TrainclastuID;
                for (int j = 0; j < dt_ProjectClassUser.Rows.Count; j++)
                {
                    TrainclastuID = int.Parse(dt_ProjectClassUser.Rows[j]["TrainclastuID"].ToString());
                    objProjectUser.DelProjectClassUser(TrainclastuID);
                }
                opUser.Del(UserID);
                if (PowerCreator.LMS.Web.Config.WebConfig.IsUseNetMeeting)
                {
                    objNetMeetingClass.WebDeleteUser(UserID);
                }
            }
        }
        string search = GetSearch();
        BinderPager(search);
        BinderRpt(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btnLock_Click(object sender, EventArgs e)
    {
        //锁定选中
        for (int i = 0; i < rptUser.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptUser.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
            {
                Label lblid = (Label)rptUser.Items[i].FindControl("lblid");
                VOTUser vo = opUser.LoadUser(int.Parse(lblid.Text));
                if (vo != null)
                {
                    if (vo.UserIscheck != 2)
                    {
                        vo.UserIscheck = 2;
                        opUser.Update(vo);
                    }
                }
            }
        }
        string search = GetSearch();
        BinderRpt(search, AspNetPager1.CurrentPageIndex);
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选
        for (int i = 0; i < rptUser.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptUser.Items[i].FindControl("chkSelect");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //返回
        for (int i = 0; i < rptUser.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptUser.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        string url = "AddTeacher.aspx";
        Script.RedirectURL(url);
    }
    protected void rptUser_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "leditor")
        {
            string url = "EditorTeacher.aspx?TeacherID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName.ToLower() == "ldel")
        {
            //删除
            //if (IsLicensed("DelTeacher"))
            //{
                PowerCreator.LMS.User.Entity.VOTTeacher voTeacher = opUser.LoadTeacher(int.Parse(e.CommandArgument.ToString()));

                DataTable dt_ProjectClassUser = objProjectUser.QueryProjectClassUser(" FKTrainclastuUserID=" + voTeacher.FKTeacherUserId.ToString());

                int TrainclastuID;
                for (int j = 0; j < dt_ProjectClassUser.Rows.Count; j++)
                {
                    TrainclastuID = int.Parse(dt_ProjectClassUser.Rows[j]["TrainclastuID"].ToString());
                    objProjectUser.DelProjectClassUser(TrainclastuID);
                }
                if (opUser.Del(voTeacher.FKTeacherUserId))
                {
                    if (PowerCreator.LMS.Web.Config.WebConfig.IsUseNetMeeting)
                    {
                        objNetMeetingClass.WebDeleteUser(voTeacher.FKTeacherUserId);
                    }
                    string search = GetSearch();
                    BinderPager(search);
                    BinderRpt(search, AspNetPager1.CurrentPageIndex);
                }
            //}
            //else
            //{
            //    Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");
            //    lbl_msg.Text = "没有删除教师用户的权限";
            //}
            //string url = "DeleteTeacher.aspx?TeacherID=" + e.CommandArgument.ToString() + "&Url=TeacherManager.aspx";
            //Script.RedirectURL(url);
        }
        else if (e.CommandName.ToLower() == "lauditing")
        {
            //审核
            VOTUser voUser = opUser.LoadUser(int.Parse(e.CommandArgument.ToString()));
            if (voUser != null)
            {
                voUser.UserIscheck += 1;
                if (voUser.UserIscheck >= 3)
                    voUser.UserIscheck = 0;

                if (opUser.Update(voUser))
                {
                    string search = GetSearch();
                    BinderRpt(search, AspNetPager1.CurrentPageIndex);
                }
            }
        }
        else if (e.CommandName.ToLower() == "lpassword")
        {
            //初始化密码
            VOTTeacher voTeacher = opUser.LoadTeacher(int.Parse(e.CommandArgument.ToString()));
            VOTUser voUser = opUser.LoadUser(voTeacher.FKTeacherUserId);
            //123456默认密码
            voUser.UserPassword = "E10ADC3949BA59ABBE56E057F20F883E";
            opUser.Update(voUser);
        }
        else if (e.CommandName == "orderby")
        {
            ViewState["OrderBy"] = e.CommandArgument.ToString();
            string search = GetSearch();
            BinderRpt(search, AspNetPager1.CurrentPageIndex);
            if (ViewState["OrderType"].ToString() == " desc")
                ViewState["OrderType"] = " asc";
            else
                ViewState["OrderType"] = " desc";
        }
    }
    protected void btn_ExportUser_Click(object sender, EventArgs e)
    {
        string url = "UserExportByExcel.aspx?UserType=1";
        Script.RedirectURL(url);
    }
    protected void btn_SortSearch_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "0";
        string search = GetSearch();
        BinderPager(search);
        BinderRpt(search, 1);
    }
    protected void btn_pwd_Click(object sender, EventArgs e)
    {
        //锁定选中
        for (int i = 0; i < rptUser.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptUser.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
            {
                Label lblid = (Label)rptUser.Items[i].FindControl("lblid");
                VOTUser vo = opUser.LoadUser(int.Parse(lblid.Text));
                if (vo != null)
                {
                    vo.UserPassword = "E10ADC3949BA59ABBE56E057F20F883E";
                    opUser.Update(vo);
                }
            }
        }
    }
}
